package uit.shoeshop.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import uit.shoeshop.dto.SlideshowDTO;
import uit.shoeshop.lib.Util;
import uit.shoeshop.model.SlideshowModel;

public class ManageslideshowAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String link;
	private String sid;
	private String saction;
	private String title;
	private String content;
	private String fileUrl;
	private ArrayList<SlideshowDTO> slideshowEvent;
	private SlideshowDTO oneSlideshow;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;

    	SlideshowModel slideshow = new SlideshowModel();
    	setLink(this.request.getRequestURL().toString());
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("del") && Util.isIntNumber(getSid())){
        			if(slideshow.delSlideshow(Integer.parseInt(getSid())) == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else{
        			ret= ERROR;
        		}
        	}else{     		
        		setSlideshowEvent(slideshow.getAllEvent());
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		slideshow = null;
    	}     	
    	return ret;
    }

    public String edit() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = ERROR;

    	SlideshowModel slideshow = new SlideshowModel();

    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("change") && Util.isIntNumber(getSid())){
        			setOneSlideshow(slideshow.getEventById(Integer.parseInt(getSid())));
        			if(getOneSlideshow() != null && getOneSlideshow().getTitle()!= null)
        				ret= NONE;
        			else
        				ret=ERROR;
        		}else if(getSaction().equals("process") && Util.isIntNumber(getSid())){
        			if(getTitle() != null && getContent() != null && getFileUrl() != null){
        				if(slideshow.updateSlideshow(Integer.parseInt(getSid()), getTitle(), getContent(), getFileUrl()) == 0)
        					ret = ERROR;
        				else
        					ret = SUCCESS;        				
        			}else{
        				ret = ERROR;
        			}
        		}else{
        			ret = ERROR;
        		}
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		slideshow = null;
    	}     	
    	return ret;
    }    
    
    public String add() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;

    	SlideshowModel slideshow = new SlideshowModel();
    	setLink("manageslideshow.html");
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST") ){
        		if(getTitle() != null && getContent() != null && getFileUrl() != null){
    				if(slideshow.insertSlideshow(getTitle(), getContent(), getFileUrl()) == 0)
    					ret = ERROR;
    				else
    					ret = SUCCESS;        				
    			}else{
    				ret = ERROR;
    			}
        	}else{     		
        		setSlideshowEvent(slideshow.getAllEvent());
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		slideshow = null;
    	}  
    	
    	return ret;    	

    }
	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @param slideshowEvent the slideshowEvent to set
	 */
	public void setSlideshowEvent(ArrayList<SlideshowDTO> slideshowEvent) {
		this.slideshowEvent = slideshowEvent;
	}

	/**
	 * @return the slideshowEvent
	 */
	public ArrayList<SlideshowDTO> getSlideshowEvent() {
		return slideshowEvent;
	}

	/**
	 * @param sid the sid to set
	 */
	public void setSid(String sid) {
		this.sid = sid;
	}

	/**
	 * @return the sid
	 */
	public String getSid() {
		return sid;
	}

	/**
	 * @param saction the saction to set
	 */
	public void setSaction(String saction) {
		this.saction = saction;
	}

	/**
	 * @return the saction
	 */
	public String getSaction() {
		return saction;
	}

	/**
	 * @param oneSlideshow the oneSlideshow to set
	 */
	public void setOneSlideshow(SlideshowDTO oneSlideshow) {
		this.oneSlideshow = oneSlideshow;
	}

	/**
	 * @return the oneSlideshow
	 */
	public SlideshowDTO getOneSlideshow() {
		return oneSlideshow;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param fileUrl the fileUrl to set
	 */
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	/**
	 * @return the fileUrl
	 */
	public String getFileUrl() {
		return fileUrl;
	}	
}
